﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using IronTrishul;
using System.Diagnostics;

namespace WebTestharness.Service
{
    public partial class ParseCommand : System.Web.UI.UserControl
    {
        static Action<String> commandhandler;
        protected void Page_Load(object sender, EventArgs e)
        {
            commandhandler = new Action<string>(AppendConsole);
            MethodInfo writeMethod = typeof(DLR).GetMethod("WriteLine", new Type[] { typeof(string) });

            if (!IsPostBack)
            {

            }

        }

        protected void btnExecute_Click(object sender, EventArgs e)
        {
            string lineCommand = txtCommand.Text;
            try
            {
                ExecuteCommand(lineCommand);
            }
            catch (Exception ex)
            {
                WriteLine("Some error occuered.." + ex.Message);
            }
        }
        private void ExecuteCommand(string command)
        {
            try
            {
                Debug.WriteLine("Trying line --" + command);
                WindowProgram.RunProgram(command);
            }
            catch (Exception dcx)
            {
                Debug.WriteLine("Error -- " + dcx.ToString());

                WriteLine(Environment.NewLine + ">>Band Command of File Name, Message - >" + dcx.Message);
            }
            finally
            {
                Debug.WriteLine(Environment.NewLine + "Command Executed successfully.");
                //DataExchange();
            }
        }
        public static void WriteLine(string line)
        {
            commandhandler.Invoke(line);
        }
        private void AppendConsole(string line)
        {
            txtConsoleOut.Text += Environment.NewLine + ">>" + line;
        }
    }
}